package com.liangzhong.modules.Main.Mediator
{
	import com.common.queueloader.QueueLoader;
	import com.common.queueloader.QueueLoaderEvent;
	import com.common.queueloader.QueueVo;
	import com.common.utils.Reflection;
	import com.global.GlobalDef;
	import com.greensock.TweenLite;
	import com.liangzhong.constants.ApplicationConstants;
	import com.liangzhong.constants.NotificationConstants;
	import com.liangzhong.constants.ResourceConst;
	import com.liangzhong.events.AppEventDispatcher;
	import com.liangzhong.events.BagEvent;
	import com.liangzhong.events.FriendEvent;
	import com.liangzhong.events.GuideEvent;
	import com.liangzhong.events.MatchEvent;
	import com.liangzhong.events.ModuleEvent;
	import com.liangzhong.events.NotifierEvent;
	import com.liangzhong.events.WinebarEvent;
	import com.liangzhong.manager.ResourceManager;
	import com.liangzhong.mediator.BaseMediator;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.goods.vo.GoodsVo;
	import com.liangzhong.model.match.vo.MatchEndAwardVo;
	import com.liangzhong.model.type.GoodsType;
	import com.liangzhong.model.type.OtherGoodType;
	import com.liangzhong.modules.bag.mediator.BagMediator;
	import com.liangzhong.modules.chat.mediator.ChatMediator;
	import com.liangzhong.modules.match.mediator.MatchMediator;
	import com.liangzhong.modules.task.mediator.TaskMediator;
	import com.liangzhong.proxy.user.UserProxy;
	import com.lz.football.module.base.IComponent;
	import com.lz.football.module.playerUp.IPlayerUpView;
	import com.lz.football.module.task.ITaskFinishView;
	
	import common.IGetPropView;
	import common.matchAward.IMatchAwardView;
	import common.newspaper.ICreateClubNewspaperView;
	import common.playerUpgrade.IPlayerUpgradeView;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.text.TextField;
	import flash.utils.getTimer;
	
	import lz.controls.Alert;
	import lz.controls.managers.AnimationManager;
	import lz.controls.managers.PopUpManager;
	
	import org.puremvc.as3.interfaces.INotification;

	/**
	 *逻辑控制 
	 * @author lxh
	 * 
	 */	
	public class LogicMediator extends BaseMediator
	{
		public static const MediatorName :String = "LogicMediator";
		/**
		 *是否俱乐部升级 
		 */		
		protected var isClubUpgrade :Boolean = false;
		/**
		 *加载器 
		 */		
		private var loader :QueueLoader;
		/**
		 *加载器1 
		 */		
		private var loader1 :QueueLoader;
		
		public function LogicMediator(mediatorName:String=null, viewComponent:Object=null)
		{
			super(mediatorName, viewComponent);
		}
		/**
		 * 
		 * 添加事件
		 */		
		override public function addEventListeners():void
		{
			AppEventDispatcher.getInstance().addEventListener(BagEvent.USE_ITEM,useItemHandler);
			AppEventDispatcher.getInstance().addEventListener(WinebarEvent.GET_AWARD,wineGetAwardHandler);
			AppEventDispatcher.getInstance().addEventListener(FriendEvent.INVITE_FRIENDS,onInviteFriendHandler);
			AppEventDispatcher.getInstance().addEventListener(FriendEvent.LOOK_UP_FRIEND,onLookUpFirend);
			AppEventDispatcher.getInstance().addEventListener(MatchEvent.GET_AWARD,matchGetAwardHandler);
		}
		/**
		 *查看好友信息 
		 * @param event
		 * 
		 */		
		protected function onLookUpFirend(event:FriendEvent):void
		{
			this.sendNotification(ApplicationConstants.FRIEND_GETFRIENDCLUBINFO,event.param);
		}
		/**
		 *邀请为好友  
		 * @param event
		 * 
		 */
		protected function onInviteFriendHandler(event:FriendEvent):void
		{
			this.sendNotification(ApplicationConstants.FRIEND_INVITEFRIENDS,event.param);
		}
		/**
		 *比赛抽奖捕获 
		 * @param e
		 * 
		 */		
		protected function matchGetAwardHandler(e :MatchEvent):void
		{
			this.sendNotification(ApplicationConstants.Public_Match_Get_Award,e.param);
		}
		/**
		 * 
		 * 删除事件
		 */		
		override public function removeEventListeners():void
		{
			
		}
		/**
		 *使用物品 
		 * @param e
		 * 
		 */		
		private function useItemHandler(e :BagEvent):void
		{
			var goodsVo :GoodsVo = e.param;
			switch(goodsVo.type)
			{
				//无法直接使用
				case GoodsType.FOOTWEAR:
				case GoodsType.ATTRIBUTE:
				case GoodsType.MATERIAL:
				{
					Alert.showErrorTip("该物品无法直接使用");
					return;
				}
					//部分无法直接使用
				case GoodsType.OTHER:
				{
					switch(goodsVo.ntype)
					{
						case OtherGoodType.CALLINCARD:
						case OtherGoodType.FRESHCARD:
						case OtherGoodType.FUCKLYCARD:
						case OtherGoodType.MEDICALCARD:
						case OtherGoodType.PHYSICAL:
						case OtherGoodType.PROTECTCARD:
						case OtherGoodType.MORALE:
						case OtherGoodType.CUIHUAJI:
						{
							Alert.showErrorTip("该物品无法直接使用");
							return;
						}
					}
					break;
				}
			}
			this.sendNotification(ApplicationConstants.BAG_USE_ITEM,e.param);
		}
		/**
		 *酒吧获取奖励
		 * @param e
		 * 
		 */		
		private function wineGetAwardHandler(e :ModuleEvent):void
		{
			this.sendNotification(ApplicationConstants.WINEBAR_GETAWARD,e.param);
		}
		/**
		 * 捕获通知
		 * @param notification
		 * 
		 */		
		override public function handleNotification(notification:INotification):void
		{
			switch(notification.getName())
			{
				case NotificationConstants.MATCH_START_LOAD_RES://比赛开始加载资源
				{
					var mName :String = MatchMediator.MediatorName + notification.getBody().matchId;
					facade.registerMediator(new MatchMediator(mName,null,notification.getBody()));
					break;
				}
				case NotificationConstants.COMMON_GET_PROP://获取物品
				{
					AnimationManager.addItem({func:showGetPropView,data:notification.getBody()});
					break;
				}
			}
		}
		/**
		 *显示获取物品面板 
		 * @param pids
		 * 
		 */		
		private function showGetPropView(pids :Array):void
		{
			var getPropView :IGetPropView = Reflection.createInstance("common.propAward.view.GetPropView");
			getPropView.initUI();
			getPropView.data = pids;
			PopUpManager.addWindow(getPropView as DisplayObject,GlobalDef.GameWindowContainer,0,0,true);
		}
		/**
		 * 注册感兴趣的命令
		 * @return 
		 * 
		 */		
		override public function listNotificationInterests():Array
		{
			return [NotificationConstants.MATCH_START_LOAD_RES,
				NotificationConstants.COMMON_GET_PROP];
		}
		/**
		 * 
		 * 注册进行时
		 */		
		override public function onRegister():void
		{
			//setTimeout(clubLineUpEffect,1000);//test club line up
			addEventListeners();
			var userProxy :UserProxy = new UserProxy();
			facade.registerProxy(userProxy);
			//登录成功 获取用户信息
			this.sendNotification(ApplicationConstants.User_getUserInfo);
			//预加载资源
			loader = new QueueLoader();
			loader.addEventListener(QueueLoaderEvent.ITEM_COMPLETE,onLoadItemComplete);
			loader.addEventListener(QueueLoaderEvent.QUEUE_COMPLETE,onLoadQueueComplete);
			//加载器1
			loader1 = new QueueLoader();
			loader1.addEventListener(QueueLoaderEvent.ITEM_COMPLETE,onLoadItemComplete);
			loader1.addEventListener(QueueLoaderEvent.QUEUE_COMPLETE,onLoadQueueComplete1);
			
			var releaseVersion :String = "?" + GlobalDef.ReleaseVersion;
			//zip
			//var tempTime :Number = getTimer();
			AppData.getInstance().goodsManager.parseGoodsList();
			AppData.getInstance().bufferManager.parseBufferList();
			AppData.getInstance().npcClubManager.parseList();
			AppEventDispatcher.getInstance().dispatchEvt(NotifierEvent.RES_CONFIG_LOAD_COMPLETE);
			//trace("use time:",getTimer() - tempTime);
			//loader.addQueue(new QueueVo("Config_zip"),"res/config/config.zip" + releaseVersion + ".zip");
			//泡吧奖励资源
			//loader.addQueue(new QueueVo("winebaraward"),"res/swf/winebar/WineBarReward.swf" + releaseVersion + ".swf");
			//任务
			loader.addQueue(new QueueVo("task"),"res/swf/task/task.swf" + releaseVersion + ".swf");
			//队徽
			loader1.addQueue(new QueueVo("teamIcon"),"res/swf/common/teamIcon.swf" + releaseVersion + ".swf");
			//比赛加载面板
			loader.addQueue(new QueueVo("MatchLoadingRes"),"res/swf/match/Match_loading.swf" + releaseVersion + ".swf");
			//好友模块资源
			loader1.addQueue(new QueueVo("FriendRes"),"res/swf/friend/friend.swf" + releaseVersion + ".swf");
			//道具使用面板
			loader.addQueue(new QueueVo("GoodsUse"),"res/swf/common/GoodsUsePanel.swf" + releaseVersion + ".swf");
			//球员升级
			loader1.addQueue(new QueueVo("playerUp_swf"),"res/swf/common/playerUP.swf" + releaseVersion + ".swf");
			//基础组件
			loader.addQueue(new QueueVo("CompSkin"),"res/swf/common/CompSkin.swf" + releaseVersion + ".swf");
			//签到
			loader1.addQueue(new QueueVo("LoginSign"),"res/swf/LoginSign/LoginSign.swf" + releaseVersion + ".swf");
			//设置
			loader.addQueue(new QueueVo("setting"),"res/swf/common/SysSetting.swf" + releaseVersion + ".swf");
			//聊天
			loader1.addQueue(new QueueVo("chat"),"res/swf/chat/chat.swf" + releaseVersion + ".swf");
			//俱乐部升级效果
			loader.addQueue(new QueueVo("club_lv"),"res/swf/effect/ClubGrade.swf" + releaseVersion + ".swf");
			//进球效果
			loader1.addQueue(new QueueVo("goal_in"),"res/swf/effect/Goal_In.swf" + releaseVersion + ".swf");
			//比赛抽奖效果
			loader.addQueue(new QueueVo("match_award"),"res/swf/match/Match_Award_1.swf" + releaseVersion + ".swf");
			//背景
			loader.addQueue(new QueueVo("Main_bg"),"res/swf/common/Main_Bg.swf" + releaseVersion + ".swf");
			//联赛效果
			loader.addQueue(new QueueVo("trial_match_p"),"res/swf/effect/trial_match_p.swf" + releaseVersion + ".swf");
			loader.addQueue(new QueueVo("base_match_p"),"res/swf/effect/base_match_p.swf" + releaseVersion + ".swf");
			loader.addQueue(new QueueVo("champion_match_p"),"res/swf/effect/champion_match_p.swf" + releaseVersion + ".swf");
			loader.addQueue(new QueueVo("legend_match_p"),"res/swf/effect/legend_match_p.swf" + releaseVersion + ".swf");
			loader.addQueue(new QueueVo("league_match_f"),"res/swf/effect/league_match_f.swf" + releaseVersion + ".swf");
			//球衣
			loader1.addQueue(new QueueVo("cloth"),"res/swf/cloths/cloth.swf" + releaseVersion + ".swf");
			//荣誉资源
			loader1.addQueue(new QueueVo("Honour_swf"),"res/swf/club/honour.swf" + releaseVersion + ".swf");
			//聊天表情
			loader1.addQueue(new QueueVo("faces"),"res/swf/common/faces.swf" + releaseVersion + ".swf");
			//声音效果
			loader1.addQueue(new QueueVo("Sound_Effects"),"res/swf/effect/SoundEffect.swf" + releaseVersion + ".swf");
			loader1.addQueue(new QueueVo("Legend_Music"),"res/swf/effect/legendMusic.swf" + releaseVersion + ".swf");
			//主界面效果
			//loader.addQueue(new QueueVo("Main_Effects"),"res/swf/effect/Main_Effects.swf" + releaseVersion + ".swf");
			//圣诞雪景
			//loader.addQueue(new QueueVo("Main_Christmas"),"res/swf/effect/Main_Christmas.swf" + releaseVersion + ".swf");
			//帮助资源
			//loader.addQueue(new QueueVo("Helper"),"res/swf/helper/helper.swf" + releaseVersion + ".swf");
			loader.queueLoadStart();
			loader1.queueLoadStart();
		}
		/**
		 * item complete
		 * @param e
		 * 
		 */		
		private function onLoadItemComplete(e:QueueLoaderEvent):void
		{
			if(e.queueVo == null)
			{
				return;
			}
			///解析
			var resConfig :XML;
			switch(e.queueVo.queueName)
			{
				case "winebaraward"://泡吧奖励资源
				{
					
					break;
				}
				case "teamIcon"://队徽swf加载完成
				{
					ResourceManager.instance.putResource(ResourceConst.TEAM_ICON_SWF,e.content);
					AppEventDispatcher.getInstance().dispatchEvt(NotifierEvent.TEAMICON_CHANGE);
					break;
				}
				case "Main_Effects"://主场景动画
				{
					break;
				}
				case  "Main_bg"://背景
				case "Main_Christmas"://圣诞雪景
				{
					this.sendNotification(NotificationConstants.MAIN_SHOW_CHRISTMAS);
					break;
				}
				case "Helper"://教程资源
				{
					ResourceManager.instance.putResource(ResourceConst.HELPER_RESOURCE,e.content)
					break;
				}
				case "chat"://聊天
				{
					facade.registerMediator(new ChatMediator(ChatMediator.MediatorName));
					break;
				}
				case "FriendRes":
				{
					ResourceManager.instance.putResource(ResourceConst.FRIEND_SWF,e.content)
					break;
				}
				case"task"://任务
				{
					facade.registerMediator(new TaskMediator(TaskMediator.MediatorName));
					break;
				}
				case "Sound_Effects"://音效
				{
					this.sendNotification(NotificationConstants.MAIN_SHOW_ANIMATE);
					break;
				}
				case "Config_zip"://压缩资源包
				{
					//var tempTime :Number = getTimer();
					var zipList :Array = e.content;
					for each(var zipObj :Object in zipList)
					{
						resConfig = XML(zipObj.content);
						ResourceManager.instance.putResource(zipObj.name,resConfig);
					}
					//trace("use time:",getTimer() - tempTime);
					AppData.getInstance().goodsManager.parseGoodsList();
					AppData.getInstance().bufferManager.parseBufferList();
					AppData.getInstance().npcClubManager.parseList();
					AppEventDispatcher.getInstance().dispatchEvt(NotifierEvent.RES_CONFIG_LOAD_COMPLETE);
					//测试
					//AppData.getInstance().bagDataManager.goodsList 
					//	= AppData.getInstance().goodsManager.goodsList.slice(0,8);
					break;
				}
			}
		}
		/**
		 * queue complete
		 * @param e
		 * 
		 */		
		private function onLoadQueueComplete(e :QueueLoaderEvent):void
		{
			loader.removeEventListener(QueueLoaderEvent.ITEM_COMPLETE,onLoadItemComplete);
			loader.removeEventListener(QueueLoaderEvent.QUEUE_COMPLETE,onLoadQueueComplete);
			loader = null;
			//test
			//var mawVo :MatchEndAwardVo = new MatchEndAwardVo();
			//mawVo.pids = [{id:1,type:4},{id:8,type:4},{id:3,type:4},{id:4,type:4}];
			//TweenLite.delayedCall(1,matchAward,[mawVo]);
		}
		/**
		 * queue complete
		 * @param e
		 */		
		private function onLoadQueueComplete1(e :QueueLoaderEvent):void
		{
			loader1.removeEventListener(QueueLoaderEvent.ITEM_COMPLETE,onLoadItemComplete);
			loader1.removeEventListener(QueueLoaderEvent.QUEUE_COMPLETE,onLoadQueueComplete);
			loader1 = null;
		}
		/**
		 *赛后抽奖 
		 * 
		 */		
		private function matchAward(objP:*):void
		{
			var matchAwardView :IMatchAwardView = Reflection.createInstance("common.matchAward.view.MatchAwardView");
			if(matchAwardView)
			{
				matchAwardView.data = objP;
				matchAwardView.initUI();
				PopUpManager.addWindow(matchAwardView as DisplayObject,GlobalDef.GameWindowContainer,0,0,true);
			}
			else
			{
				AnimationManager.remove();
			}
		}
	}
}